package system;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;







import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;




import org.joda.time.DateTime;
import org.joda.time.Minutes;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;







import system.DataBaseConnection;
import vcpClasses.Client;
import vcpClasses.SubscribedClient;



public class DataBaseConnectionTest {
	/**
	 * this is the test class
	 * 
	 * @author Gil Levinson
	 * @author Boris Fradlis
	 * @author Yogev Levi
	 * @author Hila Porat
	 *
	 */
	private DataBaseConnection db;
	private SubscribedClient subscribedClient;
	private Client client;
	private Client expectedOccasional;
	private Client expectedSub;

	@Before
	public void setUp() throws Exception {
		 db=new DataBaseConnection();
		
		client =new Client();
		client.setClientNumber(5);
		client.setNumberOfVehicles(1);
		client.setClientPhone("52666666");
		client.setUserName("AviR");
		client.setClientBalance(100);
		client.setClientBalance(0);
		
		expectedOccasional =new Client();
		expectedOccasional.setClientNumber(5);
		expectedOccasional.setNumberOfVehicles(1);
		expectedOccasional.setClientPhone("52666666");
		expectedOccasional.setUserName("AviR");
		expectedOccasional.setClientBalance(0);
		
		expectedSub =new Client();
		expectedSub.setClientNumber(5);
		expectedSub.setNumberOfVehicles(1);
		expectedSub.setClientPhone("52666666");
		expectedSub.setUserName("AviR");
		expectedSub.setClientBalance(0);
		
		subscribedClient=new SubscribedClient ();
		subscribedClient.setSubscriptionNumber(5);
		subscribedClient.setStartDate("2014-01-14");
		subscribedClient.setClientLeaveTime("00:00:00");
		subscribedClient.setTypeOfSubscription("FULL");
		subscribedClient.setLotNumber(0);
		subscribedClient.setVehicleNumber(6666666);
	}
	
	/**
	 * test for Full subscribed Cost for order with correct input
	 * rate-is the price for hour 
	 * rate=4;
	 */
	@Test
	public void testFullSubCost1() 
	{
		float rate=4;
		float cost =db.fullSubCost(rate); // CHANGED FOT TESTING - PART 1
		cost=cost+client.getClientBalance();
		client.setClientBalance(cost);
		expectedSub.setClientBalance(-288);
		Assert.assertTrue(expectedSub.getClientBalance()== client.getClientBalance());
	}
	/**
	 * test for Full subscribed Cost for order with incorrect input
	 * rate-is the price for hour if it negative we pay the costumer
	 * rate=-4;
	 */
	@Test
	public void testFullSubCost2() 
	{
		float rate=-4;
		float cost =db.fullSubCost(rate); // CHANGED FOT TESTING - PART 1
		cost=cost+client.getClientBalance();
		client.setClientBalance(cost);
		expectedSub.setClientBalance(288);
		Assert.assertTrue(expectedSub.getClientBalance()== client.getClientBalance());
	}
	/**
	 * Testing for order with incorrect input
	 * rate=5;price for hour
	 * diff=1; not relevant
	 * orderDate- is the time the client expect to live 
	 * currentDate-is the current time 
	 * 
	 * orderDate < currentDate
	 */
	@Test
	public void testCalculateCasualCost1() {
		float rate=5;
		float diff=1;
		float cost=0;
		Date date = new Date();	
		DateTime currentDate = new DateTime(date);	
		DateTime orderDate;
		DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");	
		orderDate = formatter.parseDateTime("15/01/2014" + " " + "11:00:00");
		float expectedCost =  (float) (Minutes.minutesBetween(orderDate,currentDate).getMinutes()/60.0*rate*-1*diff);
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		expectedOccasional.setClientBalance(Float.valueOf(twoDForm.format(expectedCost)));	
		cost =db.calculateCasualCost(orderDate,currentDate, rate, diff); // CHANGED FOT TESTING - PART 1
		Float.valueOf(twoDForm.format(cost));	
		cost=client.getClientBalance()+cost;
		client.setClientBalance(cost);
		Assert.assertTrue(expectedOccasional.getClientBalance()==client.getClientBalance());
	}
	/**
	 * Testing for order with incorrect input
	 * orderDate-is the time the client expect to live 
	 * currentDate-is the current time 
	 * diff=1; not relevant
	 * rate=5; price for hour
	 * orderDate = currentDate
	 */
	@Test
	public void testCalculateCasualCost2() {
		float rate=5;
		float diff=1;
		float cost=0;
		Date date = new Date();	
		DateTime currentDate = new DateTime(date);	
		DateTime orderDate=currentDate;
		float expectedCost =  (float) (Minutes.minutesBetween(orderDate,currentDate).getMinutes()/60.0*rate*-1*diff);
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		expectedOccasional.setClientBalance(Float.valueOf(twoDForm.format(expectedCost)));	
		cost =db.calculateCasualCost(orderDate,currentDate, rate, diff); // CHANGED FOT TESTING - PART 1
		Float.valueOf(twoDForm.format(cost));	
		cost=client.getClientBalance()+cost;
		client.setClientBalance(cost);
		Assert.assertTrue(expectedOccasional.getClientBalance()==client.getClientBalance());
	}
	/**
	 * Testing for order with input correctly and wrong rate 
	 * if the rate is negative we will pay the client
	 * 
	 * diff=1; not relevant
	 * rate=-5;
	 * orderDate- is the time the client expect to live 
	 * currentDate-is the current time 
	 * 
	 * orderDate > currentDate
	 * rate=-5;
	 */
	@Test
	public void testCalculateCasualCost3() {
		float rate=-5;
		float diff=1;
		float cost=0;
		Date date = new Date();	
		DateTime currentDate = new DateTime(date);	
		DateTime orderDate;
		DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");	
		orderDate = formatter.parseDateTime("7/02/2014" + " " + "11:00:00");
		float expectedCost =  (float) (Minutes.minutesBetween(orderDate,currentDate).getMinutes()/60.0*rate*-1*diff);
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		expectedOccasional.setClientBalance(Float.valueOf(twoDForm.format(expectedCost)));	
		cost =db.calculateCasualCost(orderDate,currentDate, rate, diff); // CHANGED FOT TESTING - PART 1
		Float.valueOf(twoDForm.format(cost));	
		cost=client.getClientBalance()+cost;
		client.setClientBalance(cost);
		Assert.assertTrue(expectedOccasional.getClientBalance()==client.getClientBalance());
	}
	/**
	 * Testing for order with input correctly 
	 * diff=1; not relevant
	 * rate=5; price for hour
	 * orderDate- is the time the client expect to live 
	 * currentDate-is the current time 
	 * 
	 * orderDate > currentDate
	 */
	@Test
	public void testCalculateCasualCost4() {
		float rate=5;
		float diff=1;
		float cost=0;
		Date date = new Date();	
		DateTime currentDate = new DateTime(date);	
		DateTime orderDate;
		DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");	
		orderDate = formatter.parseDateTime("7/02/2014" + " " + "11:00:00");
		float expectedCost =  (float) (Minutes.minutesBetween(orderDate,currentDate).getMinutes()/60.0*rate*-1*diff);
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		expectedOccasional.setClientBalance(Float.valueOf(twoDForm.format(expectedCost)));	
		cost =db.calculateCasualCost(orderDate,currentDate, rate, diff); // CHANGED FOT TESTING - PART 1
		Float.valueOf(twoDForm.format(cost));	
		cost=client.getClientBalance()+cost;
		client.setClientBalance(cost);
		Assert.assertTrue(expectedOccasional.getClientBalance()==client.getClientBalance());
	}

}
